#!/bin/bash

BASEDIR=`dirname $0`
CURRY_WORKDIR=${BASEDIR}/work
CXCLASS_DIR=`pushd ${BASEDIR}/utils >/dev/null && pwd`
CXCLASS=${CXCLASS_DIR}/cxclass

oneTimeSetUp()
{
    rm -Rf ${CURRY_WORKDIR}
}

#run once before each test
setUp()
{
    mkdir -p ${CURRY_WORKDIR}
}
#run once after each test
tearDown()
{
    echo "tearing down"
    rm -Rf ${CURRY_WORKDIR}
}
testEquality()
{
    #           Message             expected   actual
    assertEquals "a-ok"               1            1
}

testGenerateTenSimpleClasses()
{
    RESULTS=${PWD}/results.10
    pushd ${CURRY_WORKDIR} && curry -init && popd
    pushd ${CURRY_WORKDIR}
    sed 's/COMPOSITES=.*/COMPOSITES=/g' uservars.Makefile > uservars.temp
    cp -f uservars.temp uservars.Makefile

    iterator=0
    while ((iterator <= 10)); do
	CLASSNAME="a${iterator}"
	${CXCLASS} a${iterator}
	echo "SOURCES_${CLASSNAME}=${CLASSNAME}.cpp" >> uservars.Makefile
	echo "SLIB_${CLASSNAME}=lib${CLASSNAME}.a" >> uservars.Makefile
	echo "" >> uservars.Makefile
	sed "s/\(COMPOSITES=.*\)/\1\ ${CLASSNAME}/g" uservars.Makefile > uservars.temp.1
	cp -f uservars.temp.1 uservars.Makefile
	((iterator=$iterator+1))
    done
    popd

    TIMESTATS=timestats
    pushd ${CURRY_WORKDIR}
    echo "Initial build(10)"
    /usr/bin/time --output ${TIMESTATS} make
    cat ${TIMESTATS} |sed 's/.*system\ \(.*\)elapsed.*/\1 seconds/g' |awk '{if(NR==1)print}' > ${RESULTS}
    popd

    TIMESTATS2=timestats2
    pushd ${CURRY_WORKDIR}
    echo "Rebuild(10)"
    /usr/bin/time --output ${TIMESTATS2} make
    cat ${TIMESTATS2} |sed 's/.*system\ \(.*\)elapsed.*/\1 seconds/g' |awk '{if(NR==1)print}' >>${RESULTS}
    popd
}
testGenerateFIFTYSimpleClasses()
{
    RESULTS=${PWD}/results.50
    pushd ${CURRY_WORKDIR} && curry -init && popd
    pushd ${CURRY_WORKDIR}
    sed 's/COMPOSITES=.*/COMPOSITES=/g' uservars.Makefile > uservars.temp
    cp -f uservars.temp uservars.Makefile

    iterator=0
    while ((iterator <= 50)); do
	CLASSNAME="a${iterator}"
	${CXCLASS} a${iterator}
	echo "SOURCES_${CLASSNAME}=${CLASSNAME}.cpp" >> uservars.Makefile
	echo "SLIB_${CLASSNAME}=lib${CLASSNAME}.a" >> uservars.Makefile
	echo "" >> uservars.Makefile
	sed "s/\(COMPOSITES=.*\)/\1\ ${CLASSNAME}/g" uservars.Makefile > uservars.temp.1
	cp -f uservars.temp.1 uservars.Makefile
	((iterator=$iterator+1))
    done
    popd

    TIMESTATS=timestats
    pushd ${CURRY_WORKDIR}
    echo "Initial build(50)"
    /usr/bin/time --output ${TIMESTATS} make
    cat ${TIMESTATS} |sed 's/.*system\ \(.*\)elapsed.*/\1 seconds/g' |awk '{if(NR==1)print}' > ${RESULTS}
    popd

    TIMESTATS2=timestats2
    pushd ${CURRY_WORKDIR}
    echo "Rebuild(50)"
    /usr/bin/time --output ${TIMESTATS2} make
    cat ${TIMESTATS2} |sed 's/.*system\ \(.*\)elapsed.*/\1 seconds/g' |awk '{if(NR==1)print}' >>${RESULTS}
    popd
}


testGenerateOneHundredSimpleClasses()
{
    RESULTS=${PWD}/results.100
    pushd ${CURRY_WORKDIR} && curry -init && popd
    pushd ${CURRY_WORKDIR}
    sed 's/COMPOSITES=.*/COMPOSITES=/g' uservars.Makefile > uservars.temp
    cp -f uservars.temp uservars.Makefile

    iterator=0
    while ((iterator <= 100)); do
	CLASSNAME="a${iterator}"
	${CXCLASS} a${iterator}
	echo "SOURCES_${CLASSNAME}=${CLASSNAME}.cpp" >> uservars.Makefile
	echo "SLIB_${CLASSNAME}=lib${CLASSNAME}.a" >> uservars.Makefile
	echo "" >> uservars.Makefile
	sed "s/\(COMPOSITES=.*\)/\1\ ${CLASSNAME}/g" uservars.Makefile > uservars.temp.1
	cp -f uservars.temp.1 uservars.Makefile
	((iterator=$iterator+1))
    done
    popd

    TIMESTATS=timestats
    pushd ${CURRY_WORKDIR}
    echo "Initial build (100)"
    /usr/bin/time --output ${TIMESTATS} make
    cat ${TIMESTATS} |sed 's/.*system\ \(.*\)elapsed.*/\1 seconds/g' |awk '{if(NR==1)print}' > ${RESULTS}
    popd

    TIMESTATS2=timestats2
    pushd ${CURRY_WORKDIR}
    echo "Rebuild (100)"
    /usr/bin/time --output ${TIMESTATS2} make
    cat ${TIMESTATS2} |sed 's/.*system\ \(.*\)elapsed.*/\1 seconds/g' |awk '{if(NR==1)print}' >> ${RESULTS}
    popd
}

testGenerateOneThousandSimpleClasses()
{
    RESULTS=${PWD}/results.1000
    pushd ${CURRY_WORKDIR} && curry -init && popd
    pushd ${CURRY_WORKDIR}
    sed 's/COMPOSITES=.*/COMPOSITES=/g' uservars.Makefile > uservars.temp
    cp -f uservars.temp uservars.Makefile

    iterator=0
    while ((iterator <= 1000)); do
	CLASSNAME="a${iterator}"
	${CXCLASS} a${iterator}
	echo "SOURCES_${CLASSNAME}=${CLASSNAME}.cpp" >> uservars.Makefile
	echo "SLIB_${CLASSNAME}=lib${CLASSNAME}.a" >> uservars.Makefile
	echo "" >> uservars.Makefile
	sed "s/\(COMPOSITES=.*\)/\1\ ${CLASSNAME}/g" uservars.Makefile > uservars.temp.1
	cp -f uservars.temp.1 uservars.Makefile
	((iterator=$iterator+1))
    done
    popd

    TIMESTATS=timestats
    pushd ${CURRY_WORKDIR}
    echo "Initial build (1000)"
    /usr/bin/time --output ${TIMESTATS} make
    cat ${TIMESTATS} |sed 's/.*system\ \(.*\)elapsed.*/\1 seconds/g' |awk '{if(NR==1)print}' > ${RESULTS}
    popd

    TIMESTATS2=timestats2
    pushd ${CURRY_WORKDIR}
    echo "Rebuild (1000)"
    /usr/bin/time --output ${TIMESTATS2} make
    cat ${TIMESTATS2} |sed 's/.*system\ \(.*\)elapsed.*/\1 seconds/g' |awk '{if(NR==1)print}' >> ${RESULTS}
    popd

}


source ${BASEDIR}/shunit2-2.1.5/src/shell/shunit2
